//初始化页面
$(document).ready(function(){ 
	getIndexTestList(1);
});
//查询
function search(){
	getIndexTestList(1);
}
//重置
function reset(){
	$('#name').val('');
	getIndexTestList(1);
}
 //图标
function a(){
	$("input[name='tb']").click(function(){
		var a = $(this).val();
		$('#b').attr('class',a);
		$('#tb').attr('value',a);
	});
}

//获取数据库信息，并输出显示
function getIndexTestList(page) {
	var name=$('#name').val();
	$.ajax({	
			type : "post", 
			url : "/get_cd_admin.action",
			data:{page:page,name:name},
			success : function(result){
				if(result.success){
					var list = result.data;
					var r="";
					r += '<table class="table table-striped table-bordered table-condensed">'+
							'<thead>'+
								'<th style="width:10%">序号</th>'+
								'<th style="width:15%">菜单名称</th>'+
								'<th style="width:45%;">链接地址</th>'+
								'<th style="width:8%">图标</th>'+
								'<th style="width:12%">菜单类型</th>'+
								'<th style="width:10%">操作</th>'+
							'</thead>';
					if(list.length>0){
						r+='<tbody>';
						for(var i=0;i<list.length;i++){
							r+='<tr style="text-align:center">'+
									'<td>'+(i+1)+'</td>'+
									'<td>'+list[i].name+'</td>'+
									'<td style="text-align:left">'+list[i].lj+'</td>'+
									'<td><i class="'+list[i].tb+'"></i></td>'+
									'<td>'+list[i].type+'</td>'+
									'<td><a class="upd" onclick="edit('+list[i].id+')">修改</a>&nbsp;<a class="del" onclick="del('+list[i].id+')">删除</a></td>'+
								'</tr>';
						}
						r+='</tbody>';
				}else{
					r+='<tr><td colspan="6" style="text-align:center;color:red;">暂无相关信息</td></tr>';
					r+='</tbody>';
				}
					r+='</table>';
					$("#lise").html(r);
//					分页
					if(list.length > 0){
	                	$('#fye').getPageStr(page, 5, result.count,"getIndexTestList",'');
	                }else{
	                	$('#fye').empty();
	                }
				}
			
		}
	});
}

//删除
function del(id){
	if(confirm('确认修改此条信息？')){
		$.ajax({	
			type : "post", 
			url : "/del_cd_admin.action",
			data:{id:id},
			success : function(result) {
				if(result.success){
					alert("删除成功！");
					getIndexTestList(1);
				}else{
					alert("删除失败！");
				}
			}
		});
	}else{
		alert('删除已取消');
	}
}

//修改
//显示修改页面
function b(){
	var type = $("#b").attr("class");
	alert(type);
	$(":radio[name='tb'][value='"+type+"']").attr("checked","checked"); 
}
function tb(){
	$('#hh').toggle(220);//切换元素显隐状态
}
function edit(id){
	$.ajax({	
		type : "post", 
		url : "/edit_cd_admin.action",
		data:{id:id},
		success : function(result) {
			if(result.success){
				var bean = result.data;
				var str= '<form id="update"><div style="text-align: center"><p>'+'<br/>'+'<br/>'+
				 			'<input class="form-control" type="hidden" id="id" name="id" value="'+id+'"/>'+
						    '<span class="color_333">菜单名称:</span>'+
						    '<input class="form-control" type="text" id="name" name="name" value="'+bean.name+'"/>'+'<br/>'+
						    '<span class="color_333">链接地址:</span>'+
						    '<input class="form-control" type="text" id="lj" name="lj" value="'+bean.lj+'"/>'+
						    '<br/>'+'<br/>'+
						    '<span class="color_333"style="margin:0px 0px 0px -290px">图&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;标:</span>'+
							'<input  type="hidden" id="tb" name="tb" value="'+bean.tb+'"class="form-control"/>'+
							'<div onclick="javascript:tb()" class="form-control" style="display:inline-block;margin:-24px 0px 0px 62px;width:260px;height:16px;text-align:center;border: 1px solid #dddddd;">'+
								'<i class="'+bean.tb+'" id="b" style="color:#8e8e8e;"></i>'+
							'</div>'+
					   '</p>'+
					   '<div id="hh" style="padding-left:62px;display:none;">'+
					   '<input name="tb" value="fa fa-user fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-user fa-3x"></i>'+  
					   '<input name="tb" value="fa fa-star fa-3x" type="radio" onclick="javascript:a()" /><i class="fa fa-star fa-3x"></i>'+
					   '<input name="tb" value="fa fa-crosshairs fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-crosshairs fa-3x"></i>'+
					   '<input name="tb" value="fa fa-list-ul fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-list-ul fa-3x"></i>'+
					   '<input name="tb" value="fa fa-bookmark fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-bookmark fa-3x"></i>  <br/>'+
					   '<input name="tb" value="fa fa-share-alt fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-share-alt fa-3x"></i>'+   
					   '<input name="tb" value="fa fa-font fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-font fa-3x"></i>'+
					   '<input name="tb" value="fa fa-wrench fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-wrench fa-3x"></i>'+      
					   '<input name="tb" value="fa fa-gears fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-gears fa-3x"></i>'+
					   '<input name="tb" value="fa fa-tags fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-tags fa-3x"></i> <br/>'+      
					   '<input name="tb" value="fa fa-list-alt fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-list-alt fa-3x"></i>'+
						'</div>'+
					   '<p>'+
					        '<span class="color_333">菜单类型:</span>'+
					        '<select class="form-control" id="type" name="type" style="width:276px;margin-top:20px;">';//
						if((bean.type)=='前台菜单'){
							 str += '<option value="前台菜单"  selected="selected">前台菜单</option>'+
		         					'<option value="后台菜单">后台菜单</option>';
						}else{
							str += '<option value="后台菜单" selected="selected">后台菜单</option>'+
				         			'<option value="前台菜单">前台菜单</option>';
						}
					       str += '</select>'+
					   '</p>'+'<br/>'+
					   '<a class="panele"  href="javascript:tiaojian('+id+')">保存</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
			           '<a class="panele" href="/cd_admin/jsp/cd_admin.jsp"> 返回</a>'+
					   '</div></form>';
				$("#top").html(str);
			}
		
		}
	});
//	b();
}
//将修改内容提交数据库
function update(){
	if(confirm('确认修改此条信息？')){
		$.ajax({	
			type : "post", 
			url : "/update_cd_admin.action",
			data:$("#update").serialize(),
			success : function(result) {
				if(result.success){
					alert("修改成功！");
					window.location.href="/cd_admin/jsp/cd_admin.jsp";
				}else{
					alert("修改失败！");
				}
			}
		});
	}else{
		alert("修改取消");
	}	
}
//添加
//显示添加页面
function add(){
var str='';
str += '<form id="add"><div style="text-align: center"><p>'+'<br/>'+'<br/>'+
			'<span class="color_333">菜单名称:</span>'+
			'<input class="form-control" type="text" id="name" name="name"/>'+'<br/>'+
			'<span class="color_333">链接地址:</span>'+
			'<input class="form-control" type="text" id="lj" name="lj"onclick="javascript:lje()"/>'+
			'<br/>'+'<br/>'+
			'<span class="color_333" style="margin:0px 0px 0px -290px">图&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;标:</span>'+
			'<input  type="hidden" id="tb" name="tb" value=""class="form-control"/>'+
			'<div onclick="javascript:tb()" class="form-control" style="display:inline-block;margin:-24px 0px 0px 62px;width:260px;height:16px;text-align:center;border: 1px solid #dddddd;">'+
				'<i class="fa fa-user fa-3x" id="b" style="color:#8e8e8e;"></i>'+
			'</div>'+
	   '</p>'+
	   '<div id="hh" style="padding-left:62px;display:none;">'+
	   '<input name="tb" value="fa fa-user fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-user fa-3x"></i>'+  
	   '<input name="tb" value="fa fa-star fa-3x" type="radio" onclick="javascript:a()" /><i class="fa fa-star fa-3x"></i>'+
	   '<input name="tb" value="fa fa-crosshairs fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-crosshairs fa-3x"></i>'+
	   '<input name="tb" value="fa fa-list-ul fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-list-ul fa-3x"></i>'+
	   '<input name="tb" value="fa fa-bookmark fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-bookmark fa-3x"></i>  <br/>'+
	   '<input name="tb" value="fa fa-share-alt fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-share-alt fa-3x"></i>'+   
	   '<input name="tb" value="fa fa-font fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-font fa-3x"></i>'+
	   '<input name="tb" value="fa fa-wrench fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-wrench fa-3x"></i>'+      
	   '<input name="tb" value="fa fa-gears fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-gears fa-3x"></i>'+
	   '<input name="tb" value="fa fa-tags fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-tags fa-3x"></i> <br/>'+      
	   '<input name="tb" value="fa fa-list-alt fa-3x" type="radio" onclick="javascript:a()"/><i class="fa fa-list-alt fa-3x"></i>'+
		'</div>'+
	   '<p>'+
	        '<span class="color_333">菜单类型:</span>'+
	        '<select class="form-control" id="type" name="type"  style="width:276px;margin-top:20px;">'+//	   margin-top:20px;
	         	'<option value="前台菜单">前台菜单</option>'+
	         	'<option value="后台菜单">后台菜单</option>'+
	        '</select>'+
	   '</p>'+'<br/>'+
	   '<a class="panele"  href="javascript:tiaojian(0)">保存</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
       '<a class="panele" href="" onclick=""> 返回</a>'+
	   '</div></form>';
$("#top").html(str);
}

//执行数据库添加操作
function save(){
	//添加判断，若输入框内有为空的，弹出提示框，暂不进行保存
	$.ajax({	
		type : "post", 
		url : "/save_cd_admin.action",
		data:$("#add").serialize(),
		success : function(result) {
			if(result.success){
				alert("添加成功！");
				window.location.href="/cd_admin/jsp/cd_admin.jsp";
			}else{
				alert("添加失败！");
			}
		
		}
	});
}


